﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 库区信息
    /// </summary>
    [ApiDescriptionSettings("基础信息", Name = "WmsArea", Order = 100)]
    [Route("api/[Controller]")]
    public class WmsAreaService : IWmsAreaService, IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsArea,MasterDbContextLocator> _wmsAreaRep;


        public WmsAreaService(
            IRepository<WmsArea,MasterDbContextLocator> wmsAreaRep
        )
        {
            _wmsAreaRep = wmsAreaRep;
        }

        /// <summary>
        /// 分页查询库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<WmsAreaOutput>> Page([FromQuery] WmsAreaSearch input)
        {
            var wmsAreas = await _wmsAreaRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Areaname), u => EF.Functions.Like(u.AreaName, $"%{input.Areaname.Trim()}%"))
                                     .Where(input.Areatype != null, u => u.AreaType == input.Areatype)
                                     .Where(input.Areastatus != null, u => u.AreaStatus == input.Areastatus)
                                     .OrderBy(PageInputOrder.OrderBuilder<WmsAreaSearch>(input))
                                     .ProjectToType<WmsAreaOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return wmsAreas;
        }

        /// <summary>
        /// 增加库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task Add(AddWmsAreaInput input)
        {
            var isExit = await _wmsAreaRep.AnyAsync(n => n.AreaName == input.Areaname);
            if (isExit) throw Oops.Oh("存在的相同的库区名称！");
            var wmsArea = input.Adapt<WmsArea>();
            await _wmsAreaRep.InsertAsync(wmsArea);
        }

        /// <summary>
        /// 删除库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task Delete(DeleteWmsAreaInput input)
        {
            var wmsArea = await _wmsAreaRep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _wmsAreaRep.DeleteAsync(wmsArea);
        }

        /// <summary>
        /// 更新库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task Update(UpdateWmsAreaInput input)
        {
            var isExist = await _wmsAreaRep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var wmsArea = input.Adapt<WmsArea>();
            await _wmsAreaRep.UpdateAsync(wmsArea,ignoreNullValues:true);
        }

        /// <summary>
        /// 获取库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<WmsAreaOutput> Get([FromQuery] QueryeWmsAreaInput input)
        {
            return (await _wmsAreaRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<WmsAreaOutput>();
        }

        /// <summary>
        /// 获取库区信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<List<WmsAreaOutput>> List([FromQuery] WmsAreaInput input)
        {
            return await _wmsAreaRep.DetachedEntities.ProjectToType<WmsAreaOutput>().ToListAsync();
        }    

    }
}
